//
//  IcMaterialsMngr.m
//  IceClear
//
//  Created by P & C on 3/23/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "IcMaterialsMngr.h"


@implementation IcMaterialsMngr


-(id)initWithDefaults {
	self = [super init];
	
	if (self) {
		self->allMaterials = [[NSMutableDictionary dictionary] retain];
		[self loadAllMaterials];
	}
	
	return self;
}

-(void)loadAllMaterials {
	// Create and store list of materials known to the application

	IcMaterial* m1 = [[IcMaterial alloc] initWithName:@"nacl" title:@"Sodium Chloride (NaCl) aka Rock Salt" 
											  details:@"Common table salt.  \r(+) Affordable  \r(+) Keeps sidewalks dry  \r(-) Corrosive, badly damages concrete & vegetation  \r(-) Inneffective at low temperatures  \r" 
										   usageTypes:[NSArray arrayWithObject:[NSNumber numberWithInt:0]] 
										facilityTypes:[NSArray arrayWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1], [NSNumber numberWithInt:2], nil] 
								   lowestTemperature:-10
								   precipitationTypes:[NSArray arrayWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1], nil]
									suggestedProducts:[NSArray arrayWithObjects:[NSArray arrayWithObjects:@"Road Runner De-icing Salt", [NSURL URLWithString:@"http://www.scotwoodindustries.com/brands/17/products/14"], nil],
																				[NSArray arrayWithObjects:@"Safe Step Standard 3300", [NSURL URLWithString:@"http://www.nasalt.com/products/deicing/consumer.html"], nil],
																				nil]];
	
	IcMaterial* m15 = [[IcMaterial alloc] initWithName:@"eco-nacl" title:@"Treated Rock Salt" 
											  details:@"Traditional rock salt blended with deicing additives such as sugars from corn or sugar cane. \r(+) Eco-friendly \r(-) Expensive \r" 
											usageTypes:[NSArray arrayWithObject:[NSNumber numberWithInt:0]] 
										facilityTypes:[NSArray arrayWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1], [NSNumber numberWithInt:2], nil] 
									lowestTemperature:-18
									precipitationTypes:[NSArray arrayWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1], nil]
									 suggestedProducts:[NSArray arrayWithObjects:[NSArray arrayWithObjects:@"GEOMELT", [NSURL URLWithString:@"http://www.geomelt.ca/"], nil],
																				 [NSArray arrayWithObjects:@"THAWROX Treated Salt", [NSURL URLWithString:@"http://www.nasalt.com/products/deicing/highway/bulk-specialty.html"], nil],
																				 nil]];	
	
	IcMaterial* m2 = [[IcMaterial alloc] initWithName:@"amm-sulf" title:@"Ammonium Sulfate" 
											  details:@"(+) Fertilizer \r(-) Damages concrete \r" 
										   usageTypes:[NSArray arrayWithObject:[NSNumber numberWithInt:0]] 
										facilityTypes:[NSArray arrayWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1], [NSNumber numberWithInt:2], nil] 
									lowestTemperature:-8
								   precipitationTypes:[NSArray arrayWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1], nil]
									suggestedProducts:[NSArray arrayWithObjects:nil]];
	
	IcMaterial* m3 = [[IcMaterial alloc] initWithName:@"cacl" title:@"Calcium Chloride (CaCl)" 
											  details:@"(+) Melts ice faster than sodium chloride \r(-) Attracts moisture, surfaces slippery below -18°C \r(-) Damages concrete \r" 
										   usageTypes:[NSArray arrayWithObject:[NSNumber numberWithInt:0]] 
										facilityTypes:[NSArray arrayWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1], [NSNumber numberWithInt:2], nil] 
									lowestTemperature:-30
								   precipitationTypes:[NSArray arrayWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1], nil]
									suggestedProducts:[NSArray arrayWithObjects:[NSArray arrayWithObjects:@"Winter Heat", [NSURL URLWithString:@"http://www.scotwoodindustries.com/brands/11/products/25"], nil],
																				[NSArray arrayWithObjects:@"Safe Step Extreme 7300", [NSURL URLWithString:@"http://www.nasalt.com/products/deicing/consumer.html"], nil],
																				nil]];
	
	IcMaterial* m4 = [[IcMaterial alloc] initWithName:@"cma" title:@"Calcium Magnesium Acetate (CMA)" 
											  details:@"(+) Safest for concrete & vegetation \r(-) Works better to prevent re-icing than as ice remover \r" 
										   usageTypes:[NSArray arrayWithObject:[NSNumber numberWithInt:0]] 
										facilityTypes:[NSArray arrayWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1], [NSNumber numberWithInt:2], nil] 
									lowestTemperature:-10 
								   precipitationTypes:[NSArray arrayWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1], nil]
									suggestedProducts:[NSArray arrayWithObjects:nil]];
	
	IcMaterial* m5 = [[IcMaterial alloc] initWithName:@"mgcl" title:@"Magnesium Chloride (MgCl)" 
											  details:@"Very popular ice melter. \r(+) Melts ice faster than sodium chloride \r(+) Does not damage concrete and vegetation \r(-) Attracts moisture \r(-) Damages metals \r" 
										   usageTypes:[NSArray arrayWithObject:[NSNumber numberWithInt:0]] 
										facilityTypes:[NSArray arrayWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1], [NSNumber numberWithInt:2], nil] 
									lowestTemperature:-26
								   precipitationTypes:[NSArray arrayWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1], nil]
									suggestedProducts:[NSArray arrayWithObjects:[NSArray arrayWithObjects:@"Safe Step Extreme 8300", [NSURL URLWithString:@"http://www.nasalt.com/products/deicing/consumer.html"], nil],
																				nil]];
	
	IcMaterial* m6 = [[IcMaterial alloc] initWithName:@"kac" title:@"Potassium Acetate (KAc)" 
											  details:@"(+) Biodegradable \r(-) Corrosive \r" 
										   usageTypes:[NSArray arrayWithObject:[NSNumber numberWithInt:0]] 
										facilityTypes:[NSArray arrayWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1], [NSNumber numberWithInt:2], nil] 
									lowestTemperature:-10
								   precipitationTypes:[NSArray arrayWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1], nil]
									suggestedProducts:[NSArray arrayWithObjects:nil]];
	
	IcMaterial* m7 = [[IcMaterial alloc] initWithName:@"kcl" title:@"Potassium Chloride (KCl)" 
											  details:@"(+) Fertilizer \r(-) Damages concrete \r" 
										   usageTypes:[NSArray arrayWithObject:[NSNumber numberWithInt:0]] 
										facilityTypes:[NSArray arrayWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1], [NSNumber numberWithInt:2], nil] 
									lowestTemperature:-8 
								   precipitationTypes:[NSArray arrayWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1], nil]
									suggestedProducts:[NSArray arrayWithObjects:nil]];
	
	IcMaterial* m8 = [[IcMaterial alloc] initWithName:@"urea" title:@"Urea" 
											  details:@"(+) Fertilizer \r(-) Agricultural grade is corrosive \r" 
										   usageTypes:[NSArray arrayWithObject:[NSNumber numberWithInt:0]] 
										facilityTypes:[NSArray arrayWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1], [NSNumber numberWithInt:2], nil] 
									lowestTemperature:-8
								   precipitationTypes:[NSArray arrayWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1], nil]
									suggestedProducts:[NSArray arrayWithObjects:nil]];	
	
	
	IcMaterial* m9 = [[IcMaterial alloc] initWithName:@"nacl-liquid" title:@"Liquid Sodium Chloride" 
											  details:@"Liquid sodium chloride commonly called brine. \r" 
										   usageTypes:[NSArray arrayWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1], nil] 
										facilityTypes:[NSArray arrayWithObjects:[NSNumber numberWithInt:1], [NSNumber numberWithInt:2], nil] 
									lowestTemperature:-15
								   precipitationTypes:[NSArray arrayWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1], nil]
									suggestedProducts:[NSArray arrayWithObjects:[NSArray arrayWithObjects:@"Acubrine", [NSURL URLWithString:@"http://www.cargill.com/products/salt/winter/accubrine/index.jsp"], nil],
																				nil]];
	
	IcMaterial* m10 = [[IcMaterial alloc] initWithName:@"cacl-liquid" title:@"Liquid Calcium Chloride" 
											  details:@"Liquid calcium chloride fit for prewetting rock salt. \r Most cost-effective anti-icing agent." 
											usageTypes:[NSArray arrayWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1], nil] 
										facilityTypes:[NSArray arrayWithObjects:[NSNumber numberWithInt:1], [NSNumber numberWithInt:2], nil] 
									lowestTemperature:-33
									precipitationTypes:[NSArray arrayWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1], nil]
									 suggestedProducts:[NSArray arrayWithObjects:[NSArray arrayWithObjects:@"Ward Chemical- (click to visit)", [NSURL URLWithString:@"http://www.wardchem.com/liquid-ice-control.htm"], nil],
																				 [NSArray arrayWithObjects:@"SWI Liquid Calcium Chloride", [NSURL URLWithString:@"http://www.scotwoodindustries.com/brands/15/products/29"], nil],
																				 nil]];	
	
	IcMaterial* m11 = [[IcMaterial alloc] initWithName:@"mgcl-liquid" title:@"Liquid Magnesium Chloride" 
											  details:@"Liquid magnesium chloride with a corrosion inhibitor. \r" 
											usageTypes:[NSArray arrayWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1], nil] 
										facilityTypes:[NSArray arrayWithObjects:[NSNumber numberWithInt:1], [NSNumber numberWithInt:2], nil] 
									lowestTemperature:-30
									precipitationTypes:[NSArray arrayWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1], nil]
									 suggestedProducts:[NSArray arrayWithObjects:[NSArray arrayWithObjects:@"Freezgard", [NSURL URLWithString:@"http://www.scotwoodindustries.com/brands/12/products/26"], nil],
																				 [NSArray arrayWithObjects:@"Ice Ban", [NSURL URLWithString:@"http://www.scotwoodindustries.com/brands/13/products/27"], nil],
																				 nil]];
	
	
	IcMaterial* m12 = [[IcMaterial alloc] initWithName:@"mgcl-cacl" title:@"Magnesium Chloride and Calcium Chloride Blend" 
											  details:@"Mixture of magnesium chloride and calcium chloride. Most popular ice melter used and highly affordable. \r" 
											usageTypes:[NSArray arrayWithObject:[NSNumber numberWithInt:0]] 
										facilityTypes:[NSArray arrayWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1], [NSNumber numberWithInt:2], nil] 
									lowestTemperature:-29
									precipitationTypes:[NSArray arrayWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1], nil]
									 suggestedProducts:[NSArray arrayWithObjects:[NSArray arrayWithObjects:@"Road Runner Ice Melt", [NSURL URLWithString:@"http://www.scotwoodindustries.com/brands/1/products/5"], nil],
																				 [NSArray arrayWithObjects:@"Industrial Strength Ice Melt", [NSURL URLWithString:@"http://www.scotwoodindustries.com/brands/7/products/19"], nil],
																				 [NSArray arrayWithObjects:@"Safe Step Power 5300", [NSURL URLWithString:@"http://www.nasalt.com/products/deicing/consumer.html"], nil],
																				 nil]];
	
	IcMaterial* m13 = [[IcMaterial alloc] initWithName:@"mgcl-nacl" title:@"Sodium Chloride and Magnesium Chloride Blend" 
											  details:@"Mixture of Sodium Chloride and Magnesium Chloride. Most cost-effective de-icing salt. \r" 
											usageTypes:[NSArray arrayWithObject:[NSNumber numberWithInt:0]] 
										facilityTypes:[NSArray arrayWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1], [NSNumber numberWithInt:2], nil] 
									lowestTemperature:-20
									precipitationTypes:[NSArray arrayWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1], nil]
									 suggestedProducts:[NSArray arrayWithObjects:[NSArray arrayWithObjects:@"Safe Step Power 4300", [NSURL URLWithString:@"http://www.nasalt.com/products/deicing/consumer.html"], nil],
																				 [NSArray arrayWithObjects:@"Traction Melt CI", [NSURL URLWithString:@"http://www.scotwoodindustries.com/brands/3/products/12"], nil],
																				 nil]];
	
	IcMaterial* m14 = [[IcMaterial alloc] initWithName:@"kcl-nacl" title:@"Potassium Chloride and Sodium Chloride Blend" 
											   details:@"Mixture of Potassium chloride and Sodium Chloride. \r" 
											usageTypes:[NSArray arrayWithObject:[NSNumber numberWithInt:0]] 
										 facilityTypes:[NSArray arrayWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1], [NSNumber numberWithInt:2], nil] 
									 lowestTemperature:-12
									precipitationTypes:[NSArray arrayWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1], nil]
									 suggestedProducts:[NSArray arrayWithObjects:nil]];
	
	[allMaterials setObject:m1 forKey:m1.name];
	[allMaterials setObject:m2 forKey:m2.name];
	[allMaterials setObject:m3 forKey:m3.name];
	[allMaterials setObject:m4 forKey:m4.name];
	[allMaterials setObject:m5 forKey:m5.name];
	[allMaterials setObject:m6 forKey:m6.name];
	[allMaterials setObject:m7 forKey:m7.name];
	[allMaterials setObject:m8 forKey:m8.name];
	[allMaterials setObject:m9 forKey:m9.name];
	[allMaterials setObject:m10 forKey:m10.name];
	[allMaterials setObject:m11 forKey:m11.name];
	[allMaterials setObject:m12 forKey:m12.name];
	[allMaterials setObject:m13 forKey:m13.name];
	[allMaterials setObject:m14 forKey:m14.name];
	[allMaterials setObject:m15 forKey:m15.name];
	
	[m1 release];
	[m2 release];
	[m3 release];
	[m4 release];
	[m5 release];
	[m6 release];
	[m7 release];
	[m8 release];
	[m9 release];
	[m10 release];
	[m11 release];
	[m12 release];
	[m13 release];
	[m14 release];
	[m15 release];
}

-(IcMaterial *)materialNamed:(NSString *)n {
	return [allMaterials objectForKey:n];
}

-(NSArray *)materialsGoodToUsageType:(NSUInteger)u onFacilityType:(NSUInteger)f 
					   atTemperature:(NSUInteger)t forPrecipitationType:(NSUInteger)p {
	NSMutableArray* result = [NSMutableArray array];
	
	// Implement business logic to figure out what materials to return
	// based on their property i.e. search the allMaterials array
	for (IcMaterial * m in [allMaterials allValues]) {
		if ([m.usageTypesGoodFor containsObject:[NSNumber numberWithInt:u]]
			&& [m.facilityTypesGoodFor containsObject:[NSNumber numberWithInt:f]]
			&& [m.precipitationTypesGoodFor containsObject:[NSNumber numberWithInt:p]]
			&& m.lowestPracticalTemperature <= t) {
			[result addObject:m];
		}
	}
	
	return result;
}

-(NSArray *)allMaterials {
	return [allMaterials allValues];
}

-(NSArray *)allMaterialsGrouped {
	NSMutableArray* res = [NSMutableArray array];
	
	// dry chemicals
	[res addObject:[NSArray arrayWithObjects:[allMaterials objectForKey:@"nacl"], [allMaterials objectForKey:@"eco-nacl"], 
					[allMaterials objectForKey:@"amm-sulf"], [allMaterials objectForKey:@"cacl"], [allMaterials objectForKey:@"kcl"], 
					[allMaterials objectForKey:@"mgcl"], [allMaterials objectForKey:@"cma"], [allMaterials objectForKey:@"kac"], 
					[allMaterials objectForKey:@"urea"], nil]];
	
	// liquid chemicals
	[res addObject:[NSArray arrayWithObjects:[allMaterials objectForKey:@"nacl-liquid"], [allMaterials objectForKey:@"cacl-liquid"], 
					 [allMaterials objectForKey:@"mgcl-liquid"], nil]];
	
	// dry chemical blends
	[res addObject:[NSArray arrayWithObjects:[allMaterials objectForKey:@"mgcl-cacl"], [allMaterials objectForKey:@"mgcl-nacl"], 
					  [allMaterials objectForKey:@"kcl-nacl"], nil]];
	
	return res;
}

-(NSArray *)someDeicingMaterials {
	return [NSArray arrayWithObjects:[allMaterials objectForKey:@"nacl"], [allMaterials objectForKey:@"eco-nacl"], 
			[allMaterials objectForKey:@"cacl"], [allMaterials objectForKey:@"mgcl"], nil];
}

-(NSArray *)someAntiicingMaterials {
	return [NSArray arrayWithObjects:[allMaterials objectForKey:@"nacl-liquid"], [allMaterials objectForKey:@"cacl-liquid"], 
			[allMaterials objectForKey:@"mgcl-liquid"], nil];
}


+(IcMaterialsMngr *)materialsManager {
	IcMaterialsMngr* mngr = [[IcMaterialsMngr alloc] initWithDefaults];
	return mngr;
}


-(void)dealloc {
	[allMaterials release];
	[super dealloc];
}

@end
